/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.persistence.connection;

import java.sql.Connection;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.telosys.common.TelosysException;
import org.telosys.common.logger.TelosysClassLogger;
import org.telosys.persistence.TelosysPersistenceException;

/**
 * Connection provider implementation based on a standard JDBC DataSource<br>
 * defined in the application server and retrieved via JNDI.
 * 
 * @author Laurent GUERIN
 *  
 */

//public class SqlDataSource extends TelosysObject implements ConnectionProvider //extends SqlConnectionProvider
/* package */ class ConnectionProviderDataSource implements ConnectionProvider //extends SqlConnectionProvider
{
    //private String  _sProviderName = null ;
    
    private String  _sDataSourceName = null ;
    
    private DataSource _dataSource = null ;
    
    //-----------------------------------------------------------------------------
    private static final TelosysClassLogger logger = new TelosysClassLogger(ConnectionProviderDataSource.class);
    
    private void trace(String msg) {
    	logger.trace(msg);
    }
//    private void error(String msg) {
//    	logger.error(msg);
//    }
    
    //-----------------------------------------------------------------------------
    /**
     * Constructor
     * @param sName the connection provider name
     * @param sDataSourceName the JNDI name of the datasource ( e.g. "java:comp/env/jdbc/mydatasource" )
     * @throws TelosysException
     */
    protected ConnectionProviderDataSource( String sDataSourceName ) //throws TelosysException
    {
        //_sProviderName = sName ;
        _sDataSourceName = sDataSourceName ;
        
        //--- Try to find the DataSource via JNDI 
        Context initialContext = null ;
        Object obj = null ;

        //--- 1) Create the "Initial Context"
        try
        {
            initialContext = new InitialContext();
        } catch (NamingException e)
        {
            //throw new TelosysException ("Cannot create InitialContext !");
            throw new TelosysPersistenceException ("Cannot create InitialContext !");
        }
        
        //--- 2) Lookup the given DataSource name
        try
        {
            obj = initialContext.lookup(sDataSourceName);
        } catch (NamingException e1)
        {
            //error ("Cannot find the DataSource : lookup('" + sDataSourceName + "') !");
            //throw new TelosysException ("Cannot find the DataSource : lookup('" + sDataSourceName + "') !");
            throw new TelosysPersistenceException ("Cannot find the DataSource : lookup('" + sDataSourceName + "') !");
        }
        
        //--- 3) Cast the object to DataSource
        if ( obj instanceof DataSource)
        {
            _sDataSourceName = sDataSourceName ;
            _dataSource = (DataSource) obj ;
        }
        else
        {
            //error ("objet got with lookup('" + sDataSourceName + "') is not a DataSource instance ");
            //throw new TelosysException ("Objet got with lookup('" + sDataSourceName + "') is not a DataSource instance !");
            throw new TelosysPersistenceException ("Objet got with lookup('" + sDataSourceName + "') is not a DataSource instance !");
        }
        
        trace("SqlDataSource created.");
    }

    //-----------------------------------------------------------------------------
    /* (non-Javadoc)
     * @see org.objectweb.telosys.dal.sql.ConnectionProvider#getConnection()
     */
    public Connection getConnection() throws Exception
    {
        trace("getConnection()");
        if ( _dataSource != null )
        {
            //--- Get the connection from the JDBC DataSource
            return _dataSource.getConnection();
        }
        else
        {
            //throw new TelosysException("SqlDataSource.getConnection() : the DataSource is not set (dataSource == null) ");
            throw new Exception("Cannot get Connection - SqlDataSource.getConnection() : the DataSource is not set (dataSource == null) ");
        }
    }
    
    /**
     * Returns the DataSource resource name ( JNDI name )
     * @return
     */
    public String getDataSourceName() 
    {
        return _sDataSourceName ;
    }
    
    //-----------------------------------------------------------------------------
    /* (non-Javadoc)
     * @see org.objectweb.telosys.dal.sql.ConnectionProvider#getType()
     */
    public int getType()
    {
        return ConnectionProvider.DATASOURCE ;
    }

    //-----------------------------------------------------------------------------
    /* (non-Javadoc)
     * @see org.objectweb.telosys.dal.sql.ConnectionProvider#getName()
     */
    public String getTypeName() 
    {
        return "DataSource" ;
    }

    
//    //-----------------------------------------------------------------------------
//    /* (non-Javadoc)
//     * @see org.telosys.persistence.connection.ConnectionProvider#getInitialPoolSize()
//     */
//    public int getInitialPoolSize()
//    {
//        return -1 ;
//    }
//    
//    //-----------------------------------------------------------------------------
//	/* (non-Javadoc)
//	 * @see org.telosys.persistence.connection.ConnectionProvider#getPoolSize()
//	 */
//	public int getCurrentPoolSize() {
//        return -1 ;
//	}

    public PoolStatus getPoolStatus() 
    {
    	return new PoolStatus( 0, 0, 0, 0, getTypeName() );
    }


}